package com.happystreet.config
{
	import com.happystreet.info.JobInfo;
	import com.happystreet.manager.SettingsManager;
	import com.happystreet.types.Rewards;
	
	import flash.utils.Dictionary;

	public class JobsConfig
	{
		public static const REWARD_TYPE_INDEX:int = 0;
		public static const REWARD_VALUE_INDEX:int = 1;
		public static const REWARD_TYPE_NAME_INDEX:int = 0;
		public static const REWARD_TYPE_SYMBOL_INDEX:int = 1;
		public static const LOCKED_JOB_NEEDED_INDEX:int = 0;
		public static const LOCKED_LEVEL_NEEDED_INDEX:int = 1;
		
		public static var jobs:Object;
		public static var jobTypes:Dictionary;
		public static var rewardTypes:Dictionary;
		public static var jobIds:Array = new Array();
		public static var lockedJobs:Array = new Array();
		public static var newJob:int = 0;
		public static var leveledUpJob:int = 0;
		public static var jobLevelUpEvent:Object = null;
		public function JobsConfig()
		{
			throw (new Error("JobsConfig: this class can't be instantiated."));
		}
		
		public static function setJobsConfig(config:String):void
		{
			var job:JobInfo;
			var type_xml:XML;
			var reward_xml:XML;
			var job_xml:XML;
			var params:Array;
			var index:int;
			var level_up_reward:Array;
			var charsComp:Function = function (_arg1:String, _arg2:String, _arg3:int):int
			{
				if (((isNaN(_arg1.charCodeAt(_arg3))) || (isNaN(_arg2.charCodeAt(_arg3)))))
				{
					return (0);
				}
				if (_arg1.charCodeAt(_arg3) > _arg2.charCodeAt(_arg3))
				{
					return (1);
				}
				if (_arg1.charCodeAt(_arg3) < _arg2.charCodeAt(_arg3))
				{
					return (-1);
				}
				return (charsComp(_arg1, _arg2, (_arg3 + 1)));
			}
			var jobCompare:Function = function (_arg1:int, _arg2:int):int
			{
				return (charsComp(jobs[_arg1].name, jobs[_arg2].name, 0));
			}
			var jobs_XML:XML = new XML(config);
			var job_nodes:XMLList = jobs_XML..job;
			var reward_nodes:XMLList = jobs_XML..reward;
			var type_nodes:XMLList = jobs_XML..type;
			jobs = new Object();
			jobTypes = new Dictionary();
			var job_list_state:String = SettingsManager.loadParam(SettingsManager.JOB_LIST_STATE) as String;
			if (job_list_state)
			{
				params = job_list_state.split(",");
				leveledUpJob = params[0];
				newJob = params[1];
				SettingsManager.saveParam(SettingsManager.JOB_LIST_STATE, job_list_state);
			}
			for each (type_xml in type_nodes)
			{
				jobTypes[String(type_xml.@id)] = [type_xml.@name, type_xml.@symbol];
			}
			rewardTypes = new Dictionary();
			for each (reward_xml in reward_nodes)
			{
				rewardTypes[String(reward_xml.@id)] = [reward_xml.@name, reward_xml.@symbol];
			}
			for each (job_xml in job_nodes)
			{
				job = (jobs[String(job_xml.@id)] = new JobInfo(job_xml, jobTypes));
				jobIds.push(job_xml.@id);
				index = 1;
				for each (level_up_reward in job.levelUpRewards)
				{
					if (rewardTypes[level_up_reward[REWARD_TYPE_INDEX]][REWARD_TYPE_NAME_INDEX] == Rewards.JOB_UNLOCK)
					{
						lockedJobs[level_up_reward[REWARD_VALUE_INDEX]] = [job.id, index];
					}
					index = (index + 1);
				}
				job = null;
			}
			job_nodes = null;
			jobs_XML = null;
			reward_nodes = null;
		}
	}
}